<?php

namespace App\Console\Commands;

use App\Models\GoldCash;
use App\Models\GoldEnd;
use App\Models\GoldSplit;
use App\Models\GoldSubsidy;
use App\Models\User;
use App\Models\Order;
use App\Models\GoldBean;
use App\Models\GoldCoin;
use App\Models\Withdraw;
use App\Models\GoldRound;
use App\Models\OrderItem;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\GoldPayRecord;
use App\Models\GoldProduct;
use App\Models\GoldSeedSplit;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class CleanTestData extends Command
{
	protected $signature = 'cc:clean-test-data';

	protected $description = "Clean test data";

	public function handle()
	{
        //if (app()->environment('production')) return;

       // if ($this->confirm("Are you sure to clean all test data from DevServer?")) {

            Withdraw::truncate();
            Order::truncate();
            OrderItem::truncate();
            GoldCharge::truncate();
            GoldRecord::truncate();
            GoldProduct::truncate();
            GoldCoin::truncate();
            GoldBean::truncate();
            GoldCash::truncate();
            GoldSubsidy::truncate();
            GoldEnd::truncate();
            GoldSplit::truncate();
            GoldPayRecord::truncate();

            GoldRound::query()->each(function (GoldRound $round) {
                $round->update([
                    'opened_at' => '10:00:00',
                    'started_at' => '10:00:00',
                    'ended_at' => '23:00:00',
                ]);
            });

            User::query()->chunk(50, function (Collection $users) {

                if ($users->isEmpty()) return false;

                /**
                 * @var User $user
                 */
                foreach ($users as $user) {

                    $user->cleanStormUnpaidCache();
                    $user->cleanStormPendingCache();
                    $user->cleanStormRunningCache();
                    $user->cleanStormFinishedCache();

                    $user->update([
                        'is_gold_intern' => false,
                        'is_gold_master' => false,
                        'gold_coins' => 10000,
                        'gold_beans' => 10000,
                        'gold_cashes' => 10000,
                        'gold_subsidies' => 10000,
                        'has_given_parent_cash' => false,
                        'last_team_prize_user_id' => 0,
                        'pre_free_withdraw_quota' => 0,
                        'gold_wastes_order_at' => null,
                        'gold_wastes_subsidy_at' => null,
                        'dividend_all' => 0,
                        'dividend_day' => 0,
                        'resold_profit' => 0,
                        'dividend_parent_reward' => 0
                    ]);
                }

                return true;

            });

            $this->info('Cleaned success.');
        //}
	}
}
